package com.eqdd.yiqidian.ui.mine;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.afollestad.materialdialogs.MaterialDialog;
import com.eqdd.nextinputs.Scheme;
import com.eqdd.nextinputs.StaticScheme;
import com.eqdd.nextinputs.ValueScheme;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.UserDocumentActivityCustom;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.AreaBean;
import com.eqdd.yiqidian.bean.EditBean;
import com.eqdd.yiqidian.bean.rebuild.UserAuthInfoResultBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.DialogStringCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.BaseMultiEditActivity;
import com.eqdd.yiqidian.utils.DateUtil;
import com.eqdd.yiqidian.utils.ImageUtil;
import com.eqdd.yiqidian.utils.MultiChooseUtil;
import com.eqdd.yiqidian.utils.SingleChooseUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.ActionSheetDialog;
import com.eqdd.yiqidian.widget.ChooseAlertView;
import com.eqdd.yiqidian.widget.EditDialogFragment;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import chihane.jdaddressselector.BottomDialog;
import chihane.jdaddressselector.ISelectAble;

public class UserDocumentActivity extends BaseMultiEditActivity {
    private UserDocumentActivityCustom dataBinding;
    private String[] titles = new String[]{
            "个人基本信息",
            "姓名",
            "性别",
            "民族",
            "出生日期",
            "生日",
            "身份证号码",
            "户口性质",
            "户籍地址",
            "现住址",
            "婚否",
            "生肖",
            "身高(cm)",
            "体重(kg)",
            "血型",
            "宗教信仰",
            "政治面貌",
            "兴趣爱好",

            "联系方式",
            "个人电话",//17
            "QQ号",
            "微信号",
            "邮箱地址",
            "紧急联系人",
            "紧急联系人关系",
            "紧急联系人电话",

            "学习与技能",
            "毕业院校",//24
            "所学专业",
            "文化程度",
            "职业资格",
            "外语等级",


            "工作履历",
            "工作经历",//29
            "项目经验"//29
    };
    private String[] titleTexts = new String[]{
            "个人基本信息",
            "姓名",
            "性别",
            "民族",
            "出生日期",
            "生日",
            "身份证号码",
            "户口性质",
            "户籍地址",
            "现住址",
            "婚否",
            "生肖",
            "身高(cm)",
            "体重(kg)",
            "血型",
            "宗教信仰",
            "政治面貌",
            "兴趣爱好",

            "联系方式",
            "个人电话",//17
            "QQ号",
            "微信号",
            "邮箱地址",
            "紧急联系人",
            "紧急联系人关系",
            "紧急联系人电话",

            "学习与技能",
            "毕业院校",//24
            "所学专业",
            "文化程度",
            "职业资格",
            "外语等级",


            "工作履历",
            "工作经历",//29
            "项目经验"//29
    };
    private String[] tipTexts = new String[]{
            "个人基本信息",
            "姓名",
            "性别",
            "民族",
            "出生日期",
            "生日",
            "身份证号码",
            "户口性质",
            "户籍地址",
            "现住址",
            "婚否",
            "生肖",
            "身高(cm)",
            "体重(kg)",
            "血型",
            "宗教信仰",
            "政治面貌",
            "兴趣爱好",
            "联系方式",
            "个人电话",//17
            "QQ号",
            "微信号",
            "邮箱地址",
            "紧急联系人",
            "紧急联系人关系",
            "紧急联系人电话",

            "学习与技能",
            "毕业院校",//24
            "所学专业",
            "文化程度",
            "职业资格",
            "外语等级",


            "工作履历",
            "工作经历",//29
            "项目经验"//29
    };
    private int[] types = new int[]{
            -1, 4, 4, 4, 4, 4, 4, 4, 4, 4,
            4, 4, 0, 0, 1, 1, 4, 0, -1, 0,
            0, 0, 0, 0, 1, 0, -1, 0, 0, 4,
            4, 4, -1, 4, 4
    };
    private String key;
    private String value;
    private boolean isUpdate;
    private HashMap<Integer, String> keys;
    private ActionSheetDialog chooseDialogZhengzhi;
    private ActionSheetDialog chooseDialogWenhuachengdu;
    private ActionSheetDialog chooseDialogZiyezhige;
    private ActionSheetDialog chooseDialogHunyin;
    private List<AreaBean> areaBean;
    private BottomDialog areaDialog;
    private String result;
    private ActionSheetDialog chooseDialogHukou;
    private UserAuthInfoResultBean userInfo;
    private ActionSheetDialog chooseDialogWaiyu;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_user_document);
        initTopTitleBar(View.VISIBLE, "我的档案");
        dataBinding.tvGonghao.setText("工号：" + user.getJobNumber());
    }

    @Override
    public void initData() {
        isTip = false;
        userInfo = (UserAuthInfoResultBean) getIntent().getSerializableExtra(Config.USER_AUTH_INFO);
        ArrayList<Scheme> schemes;
        for (int i = 0; i < titles.length; i++) {
            EditBean editBean = new EditBean();
            editBean.setTitle(titles[i]);
            editBean.setType(types[i]);
            editBean.setContent("");
            editBean.setTextTip(tipTexts[i]);
            editBean.setTextTitle(titleTexts[i]);
            if (types[i] == EditBean.TITLE) {
                editBean.setShowTopTitle(true);
                editBean.setTopTitle(titles[i]);
            }
            switch (i) {
                case 0:
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 8:
                case 11:
                    editBean.setEnter(false);
                    break;
                case 6:
                    editBean.setEnter(false);
                    editBean.setShow(false);
                    break;
                case 12://身高
                    schemes = new ArrayList<>();
                    schemes.add(StaticScheme.Digits());
                    schemes.add(ValueScheme.RangeLength(2, 3));
                    editBean.setSchemes(schemes);
                    break;
                case 13://体重
                    schemes = new ArrayList<>();
                    schemes.add(StaticScheme.Digits());
                    schemes.add(ValueScheme.RangeLength(2, 3));
                    editBean.setSchemes(schemes);
                case 14://血型
                    editBean.setChooseItem(getResources().getStringArray(R.array.blood));
                    break;
                case 15://宗教信仰
                    editBean.setChooseItem(getResources().getStringArray(R.array.belief));
                    break;
                case 17://兴趣爱好
                    schemes = new ArrayList<>();
                    schemes.add(ValueScheme.RangeLength(0, 50));
                    editBean.setSchemes(schemes);
                    break;
                case 19://个人电话
                    schemes = new ArrayList<>();
                    schemes.add(StaticScheme.ChineseMobile());
                    editBean.setSchemes(schemes);
                    break;
                case 20://qq
                    schemes = new ArrayList<>();
                    schemes.add(StaticScheme.Digits());
                    schemes.add(ValueScheme.MaxLength(12));
                    editBean.setSchemes(schemes);
                    break;
                case 21://微信
                    schemes = new ArrayList<>();
                    editBean.setSchemes(schemes);
                    break;
                case 22://邮箱地址
                    schemes = new ArrayList<>();
                    schemes.add(StaticScheme.Email());
                    editBean.setSchemes(schemes);
                    break;
                case 23://紧急联系人
                    schemes = new ArrayList<>();
                    editBean.setSchemes(schemes);
                    break;
                case 24://紧急联系人关系
                    editBean.setChooseItem(getResources().getStringArray(R.array.guanxi));
                    break;
                case 25://紧急联系人电话
                    schemes = new ArrayList<>();
                    schemes.add(StaticScheme.ChineseMobile());
                    editBean.setSchemes(schemes);
                    break;
                case 27://毕业院校
                    schemes = new ArrayList<>();
                    editBean.setSchemes(schemes);
                    break;
                case 28://所学专业
                    schemes = new ArrayList<>();
                    editBean.setSchemes(schemes);
                    break;
                case 31://外语等级
                    schemes = new ArrayList<>();
                    editBean.setSchemes(schemes);
                    break;

            }
            editBeans.add(editBean);
        }

        keys = new HashMap<>();
        keys.put(7, "uhousetype");
        keys.put(8, "uhouseadress");
        keys.put(9, "upadress");
        keys.put(10, "umarry");
        keys.put(12, "uheigh");
        keys.put(13, "uweigh");
        keys.put(14, "ublood");
        keys.put(15, "ubelief");
        keys.put(16, "upoliticstate");
        keys.put(17, "uinterest");
        keys.put(19, "uptel");
        keys.put(20, "uqq");
        keys.put(21, "uwchat");
        keys.put(22, "umail");
        keys.put(23, "ucontactname");
        keys.put(24, "uscontactrelat");
        keys.put(25, "uscontact");
        keys.put(27, "ugrad");
        keys.put(28, "umajor");
        keys.put(29, "uedu");
        keys.put(30, "upskill");
        keys.put(31, "uforeignclass");
        updateView(userInfo);
    }

    private void updateView(UserAuthInfoResultBean userAllInfo) {
        ImageUtil.setCircleImage(userAllInfo.getUiphoto(), dataBinding.ivHead);

        editBeans.get(1).setContent(userAllInfo.getUname());
        editBeans.get(2).setContent(userAllInfo.getUsex() == 0 ? "男" : "女");
        editBeans.get(3).setContent(userAllInfo.getUnation());
        editBeans.get(4).setContent(DateUtil.format(userAllInfo.getUdate(), "yyyy-MM-dd"));
        editBeans.get(5).setContent(userAllInfo.getUrdate());
        editBeans.get(6).setContent(userAllInfo.getUidnum());
        editBeans.get(7).setContent(userAllInfo.getUhousetype());
        editBeans.get(8).setContent(userAllInfo.getUhouseadress());
        editBeans.get(9).setContent(userAllInfo.getUpadress());
        editBeans.get(10).setContent(userAllInfo.getUmarry());
        editBeans.get(11).setContent(DateUtil.getZodiac(userAllInfo.getUdate()));
        editBeans.get(12).setContent(userAllInfo.getUheigh() + "");
        editBeans.get(13).setContent(userAllInfo.getUweigh() + "");
        editBeans.get(14).setContent(userAllInfo.getUblood());
        editBeans.get(15).setContent(userAllInfo.getUbelief());
        editBeans.get(16).setContent(userAllInfo.getUpoliticstate());
        editBeans.get(17).setContent(userAllInfo.getUinterest());

        editBeans.get(19).setContent(userAllInfo.getUptel());
        editBeans.get(20).setContent(userAllInfo.getUqq());
        editBeans.get(21).setContent(userAllInfo.getUwchat());
        editBeans.get(22).setContent(userAllInfo.getUmail());
        editBeans.get(23).setContent(userAllInfo.getUcontactname());
        editBeans.get(24).setContent(userAllInfo.getUscontactrelat());
        editBeans.get(25).setContent(userAllInfo.getUscontact());
        editBeans.get(27).setContent(userAllInfo.getUgrad());
        editBeans.get(28).setContent(userAllInfo.getUpskill());
        editBeans.get(29).setContent(userAllInfo.getUedu());
        editBeans.get(30).setContent(userAllInfo.getUmajor());
        editBeans.get(31).setContent(userAllInfo.getUforeignclass());
        editBeans.get(32).setContent(userAllInfo.getUgrad());
        if (quickAdapter != null) {
            quickAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {
        switch (p) {
            case 33:
                startActivity(WorkExpShowActivity.class);
                break;
            case 34:
                startActivity(ProjectExpShowActivity.class);
                break;
            case 7://户口性质
                if (chooseDialogHukou == null) {
                    SingleChooseUtil.getChooseDialog(UserDocumentActivity.this, 20, (sheet) -> {
                        chooseDialogHukou = sheet;
                        sheet.show();
                    }, (position, strings) -> {
                        editBeans.get(7).setContent(strings.get(position - 1));
                        quickAdapter.notifyItemChanged(7);
                        key = keys.get(7);
                        value = strings.get(position - 1);
                        isUpdate = true;
                        updateUserInfo();
                    });
                } else {
                    chooseDialogHukou.show();
                }
                break;
            case 9://现住址
                if (areaDialog == null) {
                    initAreaDialog();
                } else {
                    areaDialog.show();
                }
                break;
            case 10://婚姻
                if (chooseDialogHunyin == null) {
                    SingleChooseUtil.getChooseDialog(UserDocumentActivity.this, 22, (sheet) -> {
                        chooseDialogHunyin = sheet;
                        sheet.show();
                    }, (position, strings) -> {
                        editBeans.get(10).setContent(strings.get(position - 1));
                        quickAdapter.notifyItemChanged(10);
                        key = keys.get(10);
                        value = strings.get(position - 1);
                        isUpdate = true;
                        updateUserInfo();
                    });
                } else {
                    chooseDialogHunyin.show();
                }
                break;
            case 16://政治面貌
                if (chooseDialogZhengzhi == null) {
                    SingleChooseUtil.getChooseDialog(UserDocumentActivity.this, 15, (sheet) -> {
                        chooseDialogZhengzhi = sheet;
                        sheet.show();
                    }, (position, strings) -> {
                        editBeans.get(16).setContent(strings.get(position - 1));
                        quickAdapter.notifyItemChanged(16);
                        key = keys.get(16);
                        value = strings.get(position - 1);
                        isUpdate = true;
                        updateUserInfo();
                    });
                } else {
                    chooseDialogZhengzhi.show();
                }
                break;
            case 29://文化程度
                if (chooseDialogWenhuachengdu == null) {
                    SingleChooseUtil.getChooseDialog(UserDocumentActivity.this, 10, (sheet) -> {
                        chooseDialogWenhuachengdu = sheet;
                        sheet.show();
                    }, (position, strings) -> {
                        editBeans.get(29).setContent(strings.get(position - 1));
                        quickAdapter.notifyItemChanged(29);
                        key = keys.get(29);
                        value = strings.get(position - 1);
                        isUpdate = true;
                        updateUserInfo();
                    });
                } else {
                    chooseDialogWenhuachengdu.show();
                }
                break;
            case 30://职业资格
                if (chooseDialogZiyezhige == null) {
                    SingleChooseUtil.getChooseDialog(UserDocumentActivity.this, 16, (sheet) -> {
                        chooseDialogZiyezhige = sheet;
                        sheet.show();
                    }, (position, strings) -> {
                        editBeans.get(30).setContent(strings.get(position - 1));
                        quickAdapter.notifyItemChanged(30);
                        key = keys.get(30);
                        value = strings.get(position - 1);
                        isUpdate = true;
                        updateUserInfo();
                    });
                } else {
                    chooseDialogZiyezhige.show();
                }
                break;
            case 31://外语等级
                if (chooseDialogWaiyu == null) {
                    SingleChooseUtil.getChooseDialog(UserDocumentActivity.this, 24, (sheet) -> {
                        chooseDialogWaiyu = sheet;
                        sheet.show();
                    }, (position, strings) -> {
                        editBeans.get(31).setContent(strings.get(position - 1));
                        quickAdapter.notifyItemChanged(31);
                        key = keys.get(31);
                        value = strings.get(position - 1);
                        isUpdate = true;
                        updateUserInfo();
                    });
                } else {
                    chooseDialogWaiyu.show();
                }
                break;
        }
    }

    private void initAreaDialog() {
        OkGo.<String>get(HttpConfig.BASE_URL + HttpConfig.AREA_OPTIONS)
                .params("type", 0)
//                .cacheMode(CacheMode.IF_NONE_CACHE_REQUEST)
                .execute(new DialogStringCallBack(this) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        areaBean = new Gson().fromJson(response.body(), new TypeToken<List<AreaBean>>() {
                        }.getType());
                        areaDialog = new MultiChooseUtil().getAreaDialog(UserDocumentActivity.this, 3, areaBean, (d, a) -> {
                            areaDialog.dismiss();
                            result = "";
                            for (ISelectAble iSelectAble : a) {
                                result += iSelectAble.getName();
                            }
                            editBeans.get(9).setContent(result);
                            quickAdapter.notifyItemChanged(9);
                            new MaterialDialog.Builder(UserDocumentActivity.this)
                                    .title("请输入详细地址")
                                    .input("详细地址", "", (dialog, input) -> Log.i("yqy", "输入的是：" + input))
                                    .onPositive((dialog, which) -> {
                                        editBeans.get(9).setContent(result + dialog.getInputEditText().getText().toString());
                                        quickAdapter.notifyItemChanged(9);
                                        key = keys.get(9);
                                        value = result + dialog.getInputEditText().getText().toString();
                                        isUpdate = true;
                                        updateUserInfo();
                                    }).show();

//                            EditDialogFragment editDiglog = ChooseAlertView.getEditDiglog("请输入详细地址", "详细地址", null, contents -> {
//
//                            });
//                            editDiglog.show(getSupportFragmentManager(), "edit");
                        });
                        areaDialog.show();
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        areaBean = new Gson().fromJson(response.body(), new TypeToken<List<AreaBean>>() {
                        }.getType());
                        areaDialog = new MultiChooseUtil().getAreaDialog(UserDocumentActivity.this, 3, areaBean, (d, a) -> {
                            areaDialog.dismiss();
                            result = "";
                            for (ISelectAble iSelectAble : a) {
                                result += iSelectAble.getName();
                            }
                            EditDialogFragment editDiglog = ChooseAlertView.getEditDiglog("请输入详细地址", "详细地址", null, contents -> {
                                editBeans.get(9).setContent(result + contents.get(0));
                                quickAdapter.notifyItemChanged(9);
                                key = keys.get(9);
                                value = result + contents.get(0);
                                isUpdate = true;
                                updateUserInfo();
                            });
                            editDiglog.show(getSupportFragmentManager(), "edit");
                        });
                        areaDialog.show();
                    }
                });

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void loadNet() {
//        quickAdapter.getViewByPosition(6, R.id.ll_root).setVisibility(View.GONE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        key = null;
        value = null;
        isUpdate = false;
        if (resultCode == Config.INTENT_CONTENT) {
            value = data.getStringExtra("content");
            isUpdate = true;
            key = keys.get(requestCode);
            updateUserInfo();
        }
    }

    private void updateUserInfo() {
        if (isUpdate) {
            OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.UPDATE_USER_INFO)
                    .params("userGuid", user.getGuid())
                    .params("para", key + "='" + value + "'")
                    .execute(new DialogCallBack<HttpResult>(this) {
                        @Override
                        public void onSuccess(Response<HttpResult> response) {
                            HttpResult httpResult = response.body();
                            ToastUtil.showShort(httpResult.getMsg());
                        }
                    });
        }
    }

    @Override
    public void onChooseClick(int position) {
        System.out.println(position);
        key = null;
        value = null;
        isUpdate = false;
        if (clicked == 14 || clicked == 15 || clicked == 24) {
            key = keys.get(clicked);
            value = quickAdapter.getItem(clicked).getChooseItem()[position - 1];
            isUpdate = true;
            updateUserInfo();
        }
    }

    @Override
    public void saveData() {
    }
}
